package com.siecom.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

    public static String  getMd5(String text){
        String str = "";
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte data[] = md5.digest(text.getBytes("utf-8"));
            str = ByteTool.byte2hex(data);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return str;
    }

//    private final static String[] strHex = { "0", "1", "2", "3", "4", "5",
//                         "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
//
//              public static String getMD5One(String path) {
//                 StringBuffer sb = new StringBuffer();
//                 try {
//                         MessageDigest md = MessageDigest.getInstance("MD5");
//                         byte[] b = md.digest(FileUtils.readFileToByteArray(new File(path)));
//                         for (int i = 0; i < b.length; i++) {
//                                 int d = b[i];
//                                 if (d < 0) {
//                                         d += 256;
//                                     }
//                                 int d1 = d / 16;
//                                 int d2 = d % 16;
//                                 sb.append(strHex[d1] + strHex[d2]);
//                             }
//                     } catch (NoSuchAlgorithmException e) {
//                         e.printStackTrace();
//                     } catch (IOException e) {
//                         e.printStackTrace();
//                     }
//                 return sb.toString();
//             }


    public static String getMD5Three(String path) {
                 BigInteger bi = null;
                 try {
                         byte[] buffer = new byte[8192];
                         int len = 0;
                         MessageDigest md = MessageDigest.getInstance("MD5");
                         File f = new File(path);
                         FileInputStream fis = new FileInputStream(f);
                         while ((len = fis.read(buffer)) != -1) {
                                 md.update(buffer, 0, len);
                             }
                         fis.close();
                         byte[] b = md.digest();
                         bi = new BigInteger(1, b);
                     } catch (NoSuchAlgorithmException e) {
                         e.printStackTrace();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 return bi.toString(16);
             }

}
